package com.yunhe.authority.controller.authority;

import com.yunhe.authority.domain.authority.Organization;
import com.yunhe.authority.service.authority.OrganizationService;
import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author zhengbo
 * @date 2021/2/24 10:51
 * 组织Controller
 */
@Api(value = "organization",description = "组织维护", tags = {"Authority_Organization"})
@RestController
@RequestMapping("/organization")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 新增组织
     * @param organization 组织对象
     * @param parentId 父组织ID
     */
    @ApiOperation("新增组织")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父组织ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "stationId", value = "项目ID", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "firmId", value = "单位ID", required = false, dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @OperationRecord(menu = "组织管理", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject> add(@RequestParam(value = "parentId", required = false) Long parentId,
                                            @RequestParam(value = "stationId",required = false) Long stationId,
                                            @RequestParam(value = "firmId", required = false) Long firmId,
                                            @RequestBody Organization organization)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        Organization newOrganization = organizationService.createOrganization(firmId, stationId, parentId, organization);
        if (stationId != null || firmId != null) {
            organizationService.organizationBindAndUnbind(stationId, firmId, newOrganization.getId());
        }
        return new ResponseEntity<>(new ResultObject<>(newOrganization), HttpStatus.OK);
    }

    /**
     * 删除组织
     * @param ids 组织id列表
     */
    @ApiOperation("删除组织")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @OperationRecord(menu = "组织管理", actionType = SystemConsts.ACTION.DELETE)
    public ResponseEntity<ResultObject> delete(@RequestBody List<Long> ids) {
        organizationService.deleteOrganization(ids);
        return new ResponseEntity<>(new ResultObject<>(""), HttpStatus.OK);
    }

    /**
     * 修改组织
     * @param organization 组织对象
     * @param parentId 父组织ID
     */
    @ApiOperation("修改组织")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @OperationRecord(menu = "组织管理", actionType = SystemConsts.ACTION.EDIT)
    public ResponseEntity<ResultObject> update(@RequestParam(value = "parentId",required = false) Long parentId,
                                               @RequestBody Organization organization)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        Organization newOrganization = organizationService.updateOrganization(parentId, organization);
        return new ResponseEntity<>(new ResultObject<>(newOrganization), HttpStatus.OK);
    }

    /**
     * 根据id查询组织
     * @param id 组织ID
     */
    @ApiOperation("根据id查询组织")
    @RequestMapping(value = "/searchById/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchById(@PathVariable(value = "id") Long id) {
        Organization organization = organizationService.searchById(id);
        return new ResponseEntity<>(new ResultObject<>(organization), HttpStatus.OK);
    }

    /**
     * 根据条件查询组织列表
     * @param name 组织名称
     * @param title 组织名称
     */
    @ApiOperation("根据条件查询组织列表")
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search(@RequestParam(value = "name", required = false) String name,
                                               @RequestParam(value = "title", required = false) String title,
                                               @ModelAttribute PageParam pageParam) {
        Page<Organization> organizationPage = organizationService.searchByCondition(name, title, pageParam);
        return new ResponseEntity<>(new ResultObject<>(organizationPage), HttpStatus.OK);
    }

    /**
     * 根据项目id或单位id查询组织列表
     * @param stationId 项目ID
     */
    @ApiOperation("根据项目id或单位id查询组织列表")
    @RequestMapping(value = "/searchByStationIdOrFirmId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchByStationId(@RequestParam(value = "stationId",required = false) Long stationId,
                                                          @RequestParam(value = "firmId",required = false) Long firmId) {
        List<Organization> organizationList = organizationService.searchByStationIdOrFirmId(stationId, firmId);
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据项目id或单位id查询组织列表
     * @param stationId 项目ID
     */
    @ApiOperation("根据项目id或单位id查询组织列表")
    @RequestMapping(value = "/searchOrgByStationIdOrFirmId", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<List<Organization>>> searchOrgByStationId(@RequestParam(value = "stationId",required = false) Long stationId,
                                                          @RequestParam(value = "firmId",required = false) Long firmId) {
        List<Organization> organizationList = organizationService.searchOrgByStationId(stationId);
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据项目id或单位id查询组织列表
     * @param orgFunction 项目ID
     */
    @ApiOperation("根据组织标签查询组织")
    @RequestMapping(value = "/searchOrgByOrgFunction", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> searchOrgByOrgFunction(@RequestParam(value = "orgFunction",required = true) String orgFunction) throws ArgumentErrorException {
        List<Organization> organizationList = organizationService.searchOrgByOrgFunction(orgFunction);
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据当前登录人获取当前登录人所在归属部门或者归属子部门
     * @param firmId 项目ID
     * @param stationId 项目ID
     * @param orgFunction 项目ID
     * @param childFlag 项目ID
     */
    @ApiOperation("根据当前登录人获取当前登录人所在归属部门或者归属子部门")
    @RequestMapping(value = "/getOrgByLoginUser", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getOrgByLoginUser(@RequestParam(value = "firmId",required = false) Long firmId,
                                                          @RequestParam(value = "stationId",required = false) Long stationId,
                                                          @RequestParam(value = "orgFunction",required = true) String orgFunction,
                                                          @RequestParam(value = "childFlag",required = true) Boolean childFlag) throws ArgumentErrorException {
        Iterable<Organization> organizationList = organizationService.getOrgByLoginUser(firmId,stationId,orgFunction,childFlag,"belong");
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据当前登录人获取当前登录人所在协同部门或者协同子部门
     * @param firmId 项目ID
     * @param stationId 项目ID
     * @param orgFunction 标识
     * @param childFlag 是否需要子节点
     */
    @ApiOperation("根据当前登录人获取当前登录人所协同部门或者协同子部门")
    @RequestMapping(value = "/getSynergyOrgByLoginUser", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getSynergyOrgByLoginUser(@RequestParam(value = "firmId",required = false) Long firmId,
                                                          @RequestParam(value = "stationId",required = false) Long stationId,
                                                          @RequestParam(value = "orgFunction",required = true) String orgFunction,
                                                          @RequestParam(value = "childFlag",required = true) Boolean childFlag) throws ArgumentErrorException {
        Iterable<Organization> organizationList = organizationService.getOrgByLoginUser(firmId,stationId,orgFunction,childFlag,"synergy");
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据当前登录人获取当前登录人所负责的部门
     * @param childFlag 是否需要子节点
     */
    @ApiOperation("根据当前登录人获取当前登录人所协同部门或者协同子部门")
    @RequestMapping(value = "/getAllOrgByLoginUser", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<List<Organization>>> getAllOrgByLoginUser(@RequestParam(value = "childFlag",required = true) Boolean childFlag) throws ArgumentErrorException {
        List<Organization> organizationList = organizationService.getAllOrgByLoginUser(childFlag);
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据当前登录人获取当前登录人所负责的部门
     * @param childFlag 是否需要子节点
     */
    @ApiOperation("根据当前登录人获取当前登录人所协同部门或者协同子部门")
    @RequestMapping(value = "/getSubOrgByLoginUser", method = RequestMethod.GET)
    public ResponseEntity<ResultObject<List<Organization>>> getSubOrgByLoginUser(@RequestParam(value = "stationId",required = true) Long stationId,
                                                                                 @RequestParam(value = "userId",required = true) Long userId,
                                                                                 @RequestParam(value = "childFlag",required = true) Boolean childFlag) throws ArgumentErrorException {
        List<Organization> organizationList = organizationService.getSubOrgByLoginUser(stationId,userId,childFlag);
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }

    /**
     * 根据组织信息查询子组织信息
     * @param orgIdStr 项目ID+"-"+项目类型
     */
    @ApiOperation("根据组织信息查询子组织信息")
    @RequestMapping(value = "/getSubOrg", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getSubOrg(@RequestParam(value = "orgId",required = true) String orgIdStr) {
        Iterable<Organization> organizationList = organizationService.getSubOrg(orgIdStr);
        return new ResponseEntity<>(new ResultObject<>(organizationList), HttpStatus.OK);
    }
}
